package com.heatup.apt.management.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.heatup.apt.common.constants.OrderContants;
import com.heatup.apt.common.dao.OrderInfoMapper;
import com.heatup.apt.common.util.JsonTools;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.management.service.coach.CoachService;
import com.heatup.apt.management.service.course.CourseService;
import com.heatup.apt.management.service.custom.UserAccountRechargeService;
import com.heatup.apt.management.service.order.OrderService;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.coach.CoachInfo;
import com.heatup.apt.model.order.OrderInFoToTK;
import com.heatup.apt.model.order.OrderInfo;

/**
 * 订单管理控制器
 * 
 * @author jixiangyun 2016年9月18日 下午4:07:20
 */

@Controller
@RequestMapping("system/order")
public class OrderController extends BaseController{
	@Autowired
	private OrderService orderService;
	@Autowired
	private CourseService courseService;
	@Autowired 
	private CoachService coachService;
	@Autowired
	private UserAccountRechargeService userAccountRechargeService; 
	
	@Autowired
	private OrderInfoMapper orderInfoMapper;
	
	DecimalFormat format = new DecimalFormat("###,####0.00");
	@RequestMapping(value = "to_order_list")
	public ModelAndView toOrderList(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView model = new ModelAndView();
		model.addObject("userInfo",this.getUserInfo());
		model.setViewName("order/order-list");
		return model;
	}

	/**
	 * 打开团课订单管理界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/opendOrder")
	public ModelAndView openOrderInfo(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView model = new ModelAndView();
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		model.setViewName("/order/order");
		return model;
	}

	/**
	 * 获取团课列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getOrderList.action")
	public String getOrderList(HttpServletRequest request, HttpServletResponse response) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		List<OrderInFoToTK> list = null;
		Map<String, Object> mapWhere = new HashMap<String, Object>();
		String callback = RequestTools.RequestString(request, "callback", null);
		String course_stime = RequestTools.RequestString(request, "course_stime", null);
		String course_etime = RequestTools.RequestString(request, "course_etime", null);
		String course_id = RequestTools.RequestString(request, "course_id", null);
		String mobile = RequestTools.RequestString(request, "mobile", null);
		String store_id = RequestTools.RequestString(request, "store_id", "0");
		String type = RequestTools.RequestString(request, "type", null);
		String pageNums = RequestTools.RequestString(request, "pageNum", "1");
		String tradeId =  RequestTools.RequestString(request, "tradeId", null);
		String coachId = RequestTools.RequestString(request, "coach_id","0");
		Integer userId = RequestTools.RequestInt(request, "userId", 0);
		String userName = RequestTools.RequestString(request, "userName", null);
		mapWhere.put("course_stime", course_stime);
		mapWhere.put("course_etime", course_etime);
		mapWhere.put("course_id", course_id);
		mapWhere.put("mobile", mobile);
		mapWhere.put("store_id", store_id);
		mapWhere.put("type", type);
		mapWhere.put("tradeId",tradeId);
		mapWhere.put("coachId", coachId);
		mapWhere.put("userId", userId);
		mapWhere.put("userName", userName);
		try {

			list = orderService.getOrderListByTK(mapWhere);
			//request.getSession().setAttribute("orderListByTK",list);
			double feiyong = 0 ;
			double shifu = 0 ;
			for(OrderInFoToTK zz:list){
				feiyong =feiyong+Double.valueOf(zz.getPrime_price());
				shifu = shifu + Double.valueOf(zz.getPrice1());
			}
			
			int pageNum = Integer.valueOf(pageNums);
			if (pageNum == 1) {
				pageNum = 0;
			} else {
				pageNum = (pageNum - 1) * 20;

			}
			mapWhere.put("pageNum", pageNum);
			mapWhere.put("pageCount", 20);
			Map<String, Object> mapD = new HashMap<String, Object>();
			List<OrderInFoToTK> list1 = orderService.getOrderListByTK(mapWhere);
			mapD.put("TkData", list1);
			mapD.put("pageCount", list.size());
			mapD.put("pageNum", Integer.valueOf(pageNums));
			mapD.put("feiyong", format.format(feiyong));
			mapD.put("shifu", format.format(shifu));
			apiResultModel.setData(mapD);
			apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
			apiResultModel.setMsg("获取所有团课数据");
		} catch (Exception e) {
			apiResultModel.setData("系统错误");
			apiResultModel.setMsg("获取失败");
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			e.printStackTrace();
		}
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}

	/**
	 * 获取私教课程list
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	// TODO Auto-generated catch block
	@ResponseBody
	@RequestMapping(value = "/getOrderSJList.action")
	public String getOderSJList(HttpServletRequest request, HttpServletResponse response) {
		List<OrderInFoToTK> listData = new ArrayList<OrderInFoToTK>();
		String callback = RequestTools.RequestString(request, "callback", "");
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		Map<String, Object> mapWhere = new HashMap<String, Object>();
		String type = RequestTools.RequestString(request, "type", "");
		String course_stime = RequestTools.RequestString(request, "course_stime", null);
		String course_etime = RequestTools.RequestString(request, "course_etime", null);
		String course_id = RequestTools.RequestString(request, "course_id", null);
		String mobile = RequestTools.RequestString(request, "mobile", null);
		String store_id = RequestTools.RequestString(request, "store_id", "0");
		String pageNums = RequestTools.RequestString(request, "pageNum", "1");
		String tradeId =  RequestTools.RequestString(request, "tradeId", null);
		String coachId = RequestTools.RequestString(request, "coach_id", "0");
		Integer userId = RequestTools.RequestInt(request, "userId", 0);
		String userName = RequestTools.RequestString(request, "userName", null);
		mapWhere.put("course_stime", course_stime);
		mapWhere.put("course_etime", course_etime);
		mapWhere.put("course_id", course_id);
		mapWhere.put("mobile", mobile);
		mapWhere.put("store_id", store_id);
		mapWhere.put("type", type);
		mapWhere.put("tradeId", tradeId);
		mapWhere.put("coachId",coachId);
		mapWhere.put("userId", userId);
		mapWhere.put("userName", userName);
		try {
			listData = orderService.getOrderListBySJ(mapWhere);
			request.getSession().setAttribute("orderListBySJ",listData);
			double feiyong = 0 ;
			double shifu = 0 ;
			for(OrderInFoToTK zz:listData){
				feiyong =feiyong+Double.valueOf(zz.getPrime_price());
				shifu = shifu + Double.valueOf(zz.getPrice1());
			}
			
			int pageNum = Integer.valueOf(pageNums);
			if (pageNum == 1) {
				pageNum = 0;
			} else {
				pageNum = (pageNum - 1) * 20 ;
			}
			mapWhere.put("pageNum", pageNum);
			mapWhere.put("pageCount", 20);
			Map<String, Object> mapD = new HashMap<String, Object>();
			List<OrderInFoToTK> listData1 = orderService.getOrderListBySJ(mapWhere);
			mapD.put("SjData", listData1);
			mapD.put("pageCount", listData.size());
			mapD.put("pageNum", Integer.valueOf(pageNums));
			mapD.put("feiyong", format.format(feiyong));
			mapD.put("shifu", format.format(shifu));
			apiResultModel.setData(mapD);
			apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
			apiResultModel.setLogined(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setLogined(true);
			apiResultModel.setData("获取数据失败");
			e.printStackTrace();
		}
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);

	}

	/**
	 * 获取檬营课程list
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	// TODO Auto-generated catch block
	@ResponseBody
	@RequestMapping(value = "/getOrderMYList.action")
	public String getOderMYList(HttpServletRequest request, HttpServletResponse response) {
		List<OrderInFoToTK> listData = new ArrayList<OrderInFoToTK>();
		String callback = RequestTools.RequestString(request, "callback", "");
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		Map<String, Object> mapWhere = new HashMap<String, Object>();
		String type = RequestTools.RequestString(request, "type", "");
		String pageNums = RequestTools.RequestString(request, "pageNum", "1");
		String course_stime = RequestTools.RequestString(request, "course_stime", null);
		String course_etime = RequestTools.RequestString(request, "course_etime", null);
		String course_id = RequestTools.RequestString(request, "course_id", null);
		String mobile = RequestTools.RequestString(request, "mobile", null);
		String store_ids = RequestTools.RequestString(request, "store_id", "0");
		String tradeId =  RequestTools.RequestString(request, "tradeId", null);
		Integer userId = RequestTools.RequestInt(request, "userId", 0);
		String userName = RequestTools.RequestString(request, "userName", null);
		try {
			mapWhere.put("type", type);
			mapWhere.put("course_stime", course_stime);
			mapWhere.put("course_etime", course_etime);
			mapWhere.put("course_id", course_id);
			mapWhere.put("mobile", mobile);
			mapWhere.put("store_id", store_ids);
			mapWhere.put("tradeId", tradeId);
			mapWhere.put("userId", userId);
			mapWhere.put("userName", userName);
			listData = orderService.getOrderListByMY(mapWhere);
			request.getSession().setAttribute("orderListByMY",listData);
			double feiyong = 0 ;
			double shifu = 0 ;
			for(OrderInFoToTK zz:listData){
				feiyong =feiyong+Double.valueOf(zz.getPrime_price());
				shifu = shifu + Double.valueOf(zz.getPrice1());
			}
			
			int pageNum = Integer.valueOf(pageNums);
			if (pageNum == 1) {
				pageNum = 0;
			} else {
				pageNum = (pageNum - 1) * 20 ;
			}
			mapWhere.put("pageNum", pageNum);
			mapWhere.put("pageCount", 20);
			List<OrderInFoToTK> listData1 = orderService.getOrderListByMY(mapWhere);
			Map<String, Object> mapD = new HashMap<String, Object>();
			mapD.put("MyData", listData1);
			mapD.put("pageCount", listData.size());
			mapD.put("pageNum", Integer.valueOf(pageNums));
			mapD.put("feiyong", format.format(feiyong));
			mapD.put("shifu", format.format(shifu));
			apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
			apiResultModel.setLogined(true);
			apiResultModel.setData(mapD);
		} catch (Exception e) {
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setLogined(true);
			apiResultModel.setData("获取数据失败");
			e.printStackTrace();

		}
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);

	}

	/**
	 * 获取自助健身的列表信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getOrderZZList.action")
	public String getOderZZList(HttpServletRequest request, HttpServletResponse response) {
		List<OrderInFoToTK> listData = new ArrayList<OrderInFoToTK>();
		String callback = RequestTools.RequestString(request, "callback", "");
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		Map<String, Object> mapWhere = new HashMap<String, Object>();
		apiResultModel.setMsg("获取数据失败！");
		apiResultModel.setData("获取数据失败！");
		apiResultModel.setRet(ApiResultModel.RET_ERROR);
		String type = RequestTools.RequestString(request, "type", "");
		String pageNums = RequestTools.RequestString(request, "pageNum", "1");
		String course_stime = RequestTools.RequestString(request, "course_stime", null);
		String course_etime = RequestTools.RequestString(request, "course_etime", null);
		String title = RequestTools.RequestString(request, "title", null);
		String mobile = RequestTools.RequestString(request, "mobile", null);
		String store_id = RequestTools.RequestString(request, "store_id", "0");
		String tradeId =  RequestTools.RequestString(request, "tradeId", null);
		Integer userId = RequestTools.RequestInt(request, "userId", 0);
		String userName = RequestTools.RequestString(request, "userName", null);
		mapWhere.put("course_stime", course_stime);
		mapWhere.put("course_etime", course_etime);
		mapWhere.put("title", title);
		mapWhere.put("mobile", mobile);
		mapWhere.put("store_id", store_id);
		mapWhere.put("type", type);
		mapWhere.put("tradeId", tradeId);
		mapWhere.put("userId", userId);
		mapWhere.put("userName", userName);
		double feiyong = 0;
		double shifu = 0;
		try {
			listData = orderService.getOrderListByZZ(mapWhere);
			request.getSession().setAttribute("orderListByZZ",listData);
			for(OrderInFoToTK zz:listData){
				feiyong =feiyong+Double.valueOf(zz.getPrime_price());
				shifu = shifu + Double.valueOf(zz.getPrice1());
			}
			
			if (listData.size() > 0) {
				int pageNum = Integer.valueOf(pageNums);
				if (pageNum == 1) {
					pageNum = 0;
				} else {
					pageNum = (pageNum - 1) * 20 ;
				}
				mapWhere.put("pageNum", pageNum);
				mapWhere.put("pageCount", 20);
				List<OrderInFoToTK> listData1 = orderService.getOrderListByZZ(mapWhere);
				Map<String, Object> mapD = new HashMap<String, Object>();
				mapD.put("ZzData", listData1);
				mapD.put("pageCount", listData.size());
				mapD.put("pageNum", Integer.valueOf(pageNums));
				mapD.put("feiyong", format.format(feiyong));
				mapD.put("shifu", format.format(shifu));
				apiResultModel.setMsg("获取自助健身订单数据");
				apiResultModel.setData(mapD);
				apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
			}else{
				apiResultModel.setMsg("无自助健身退款订单");
				apiResultModel.setData("无自助健身退款订单");
				apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}

	/**
	 * 获取所有的门店
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getStoreList.action")
	public String getStoreList(HttpServletRequest request, HttpServletResponse response) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel();
		String callback = RequestTools.RequestString(request, "callback", "");
		List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
		Map<String, Object> storeMap = new HashMap<String, Object>();
		Map<String, Object> whereMap = new HashMap<String, Object>();
		listData = orderService.getStoreList(whereMap);
		if (listData.size() != 0) {
			apiResultModel.setMsg("获取门店数据");
			apiResultModel.setData(listData);
			apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
		} else {
			apiResultModel.setMsg("获取数据失败！");
			apiResultModel.setData("");
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
		}
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}

	/**
	 * 退款订单处理
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/createdRefun.action")
	public String createdRefundOrder(HttpServletRequest request, HttpServletResponse response) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel();
		String callback = RequestTools.RequestString(request, "callback", "");
		int orderId = RequestTools.RequestInt(request, "orderId", 0);
		int biaoshi = RequestTools.RequestInt(request, "biaoshi", 1);
		String refundAmt = RequestTools.RequestString(request, "refundAmt", "0");
		String remark	= RequestTools.RequestString(request, "remark", null);
		Map<String, Object> mapWhere = new HashMap<String, Object>();
		mapWhere.put("orderId", orderId);
		mapWhere.put("biaoshi", biaoshi);
		mapWhere.put("refundAmt", new BigDecimal(refundAmt));
		mapWhere.put("sysUser", this.getUserInfo());
		mapWhere.put("remark", remark);
		try {
			OrderInfo orderInfo = orderInfoMapper.selectById(orderId);
			// 判断该订单号是否存在
			if(orderInfo == null){
				apiResultModel.setData("退款失败");
				apiResultModel.setRet(ApiResultModel.RET_ERROR);
				apiResultModel.setLogined(true);
				apiResultModel.setMsg("退款失败,找不到该订单,请查证订单号的正确性");
				return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
			}
			// 全部退款和部分退款提示不能重复提申请退款
			if(OrderContants.PayStatus.REFOUND.equals(orderInfo.getPayStatus()) 
					|| OrderContants.PayStatus.PARTIAL_REFUND.equals(orderInfo.getPayStatus())) {
				apiResultModel.setData("该订单已是退款订单不要重复提交退款申请");
				apiResultModel.setRet(ApiResultModel.RET_ERROR);
				apiResultModel.setLogined(true);
				apiResultModel.setMsg("该订单已是退款订单不要重复提交退款申请");
				return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
			}
			// 未付款订单不给予退款申请
			if(OrderContants.PayStatus.UNPAID.equals(orderInfo.getPayStatus())) {
				apiResultModel.setData("该订单未付款不能申请退款");
				apiResultModel.setRet(ApiResultModel.RET_ERROR);
				apiResultModel.setLogined(true);
				apiResultModel.setMsg("该订单未付款不能申请退款");
				return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
			}
			int bool = 0;
			if(new BigDecimal(refundAmt).compareTo(BigDecimal.ZERO) == 0){
				bool = orderService.createdRefundOrder(mapWhere);
			}else{
				bool = orderService.createdRefundSplitOrder(mapWhere);
			}
			
			if (bool >= 1) {
				apiResultModel.setData("退款成功");
				apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
				apiResultModel.setLogined(true);
				apiResultModel.setMsg("退款成功");

			} else if(bool == -2){
				apiResultModel.setData("账户余额不足");
				apiResultModel.setRet(ApiResultModel.RET_ERROR);
				apiResultModel.setLogined(true);
				apiResultModel.setMsg("账户余额不足");
				
			}else{
				apiResultModel.setData("退款失败");
				apiResultModel.setRet(ApiResultModel.RET_ERROR);
				apiResultModel.setLogined(true);
				apiResultModel.setMsg("退款失败");
			}
		} catch (Exception e) {
			// TODO: handle exception
			apiResultModel.setData("系统运行异常");
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setLogined(true);
			apiResultModel.setMsg("系统运行异常");
		}

		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}

	/**
	 * 订单的取消
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateOrder")
	public String updateOrder(HttpServletRequest request, HttpServletResponse response) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel();
		String callback = RequestTools.RequestString(request, "callback", "");
		Map<String, Object> mapWhere = new HashMap<String, Object>();
		String orderId = RequestTools.RequestString(request, "orderId", null);
		String type = RequestTools.RequestString(request, "type", null);
		mapWhere.put("orderId", orderId);
		mapWhere.put("type", type);
		int return_status = orderService.updateOrder(mapWhere);
		if (return_status == 1) {
			apiResultModel.setData("订单已取消");
			apiResultModel.setMsg("订单已取消转到了被动取消中");
			apiResultModel.setLogined(true);
			apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
		} else {
			apiResultModel.setData("订单已取消");
			apiResultModel.setMsg("订单已取消转到了被动取消中");
			apiResultModel.setLogined(true);
			apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
		}
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}

	/**
	 * 打开私教课程销课界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "to_course_dispel")
	public ModelAndView tocourseDispel(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView model = new ModelAndView();
		String orderId = RequestTools.RequestString(request, "orderId", null);
		request.setAttribute("orderId", orderId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderId", orderId);
		try {
			Map<Object, Object>	resultMap =  orderService.getCourseInfoById(map);
			request.setAttribute("course_sign_list", resultMap.get("course_sign_list"));
			request.setAttribute("course_sign_count", resultMap.get("course_sign_count"));
			
			request.setAttribute("msg", "OK");
			request.setAttribute("num", resultMap.get("num"));
			request.setAttribute("orderId", orderId);
			request.setAttribute("store_list", resultMap.get("store_list"));
			
			model.setViewName("order/cancel-course");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", "ERROR");
			model.setViewName("order/order-error");
			return model;
		}
		return model;
	}
	
	
	
	/**
	 * 销课
	 * OrderController.cancel_course()<BR>
	 * <P>Author :  fangxiaowen </P>  
	 * <P>Date : 2016年10月21日 </P>
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "cancel_course",method = {RequestMethod.POST,RequestMethod.GET})
    public void cancel_course(HttpServletRequest request, HttpServletResponse response) {
        	
        	try {
        		String num = request.getParameter("num");
            	String id = request.getParameter("id");
            	String courseId = request.getParameter("courseId");
            	String orderId = request.getParameter("orderId");
            	String storeId = request.getParameter("storeId");
            	String startTime = request.getParameter("startTime");
            	String closeTime = request.getParameter("closeTime");
            	
            	Map<String,Object> resultMap= orderService.updateCancelCourse(num, id, courseId, storeId,orderId, startTime, closeTime,getUserInfo().getId());
        		responseSendMsg(response, JsonTools.jsonSer(resultMap));
        		
    		} catch (Exception e) {
    			e.printStackTrace();
    			responseSendMsg(response, "销课失败!");
    		}
    }
//
//	/**
//	 * 获取私教课程销课列表
//	 *
//	 * @param request
//	 * @param response
//	 * @return
//	 */
//	@ResponseBody
//	@RequestMapping(value = "/courseRegList.action")
//	public String courseRegList(HttpServletRequest request, HttpServletResponse response) {
//		ApiResultModel<Object> apiResultModel = new ApiResultModel();
//		String callback = RequestTools.RequestString(request, "callback", "");
//		List<CourseSchedule> listData = new ArrayList<CourseSchedule>();
//		Map<String, Object> mapWhere = new HashMap<String, Object>();
//		Map<String, Object> mapData = new HashMap<String, Object>();
//		List<Map<String, Object>> listD = new ArrayList<Map<String, Object>>();
//		String orderId = RequestTools.RequestString(request, "orderId", null);
//		mapWhere.put("orderId", orderId);
//		apiResultModel.setLogined(false);
//		apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
//		apiResultModel.setMsg("ERROR");
//		apiResultModel.setData("获取数据失败");
//		try {
//			/* listData = orderService.getCourseScheduleList(mapWhere); */
//			listD = orderService.getCourseRegList(mapWhere);
//			if (listD.size() > 0) {
//				mapData.put("list", listD);
//				mapData.put("num", listD.size());
//				apiResultModel.setLogined(true);
//				apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
//				apiResultModel.setMsg("Ok");
//				apiResultModel.setData(mapData);
//			}else{
//				mapData.put("list", listD);
//				mapData.put("num", 0);
//				apiResultModel.setLogined(true);
//				apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
//				apiResultModel.setMsg("Ok");
//				apiResultModel.setData(mapData);
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//
//		}
//		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
//	}
//
//	/**
//	 * 保存销课信息
//	 *
//	 * @param request
//	 * @param response
//	 * @return
//	 */
//	@ResponseBody
//	@RequestMapping(value = "/saveCourseReg.action")
//	public String saveCourseReg(HttpServletRequest request, HttpServletResponse response) {
//		ApiResultModel<Object> apiResultModel = new ApiResultModel();
//		String callback = RequestTools.RequestString(request, "callback", "");
//		String orderId = RequestTools.RequestString(request, "orderId", null);
//		String courseTime = RequestTools.RequestString(request, "courseTime", null);
//		String storeId = RequestTools.RequestString(request, "storeId", null);
//		Map<String, Object> mapWhere = new HashMap<String, Object>();
//		mapWhere.put("orderId", orderId);
//		mapWhere.put("courseTime", courseTime);
//		mapWhere.put("storeId", storeId);
//		int status = orderService.saveCourseReg(mapWhere);
//		apiResultModel.setLogined(true);
//		apiResultModel.setMsg("销课失败");
//		apiResultModel.setRet(ApiResultModel.RET_ERROR);
//		apiResultModel.setData("error");
//		if (status == 1) {
//			apiResultModel.setLogined(true);
//			apiResultModel.setMsg("销课成功");
//			apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
//			apiResultModel.setData("success");
//		}
//		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
//	}
	
	/**
	 * 将文件下载到指定目录
	 * @param request
	 * @param response
	 * @return
	 */
	public String exportExcelFile(HttpServletRequest request, HttpServletResponse response){
		String path = "E:\\exelc\\订单管理.xlsx";
		try {
			// path是指欲下载的文件的路径。
			File file = new File(path);
			// 取得文件名。
			String filename = file.getName();
			// 以流的形式下载文件。
			InputStream fis = new BufferedInputStream(new FileInputStream(path));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
			response.addHeader("Content-Length", "" + file.length());
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/vnd.ms-excel;charset=gb2312");
			toClient.write(buffer);
			toClient.flush();
			toClient.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return "";
	}
	
	@ResponseBody
	@RequestMapping("/get_coach_list")
	public String queryCoachList(HttpServletRequest request , HttpServletResponse response){
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		String callback = RequestTools.RequestString(request, "callback", "");
		Integer isPrivate = RequestTools.RequestInt(request, "isPrivate", 0);
		Integer isTeam = RequestTools.RequestInt(request, "isTeam", 0);
		Integer isCamp = RequestTools.RequestInt(request, "isCamp", 0);
		Map<String,Object> parm = new HashMap<>();
		try {
			parm.put("isPrivate", isPrivate);
			parm.put("isTeam", isTeam);
			parm.put("isCamp", isCamp);
			List<CoachInfo> caList = coachService.queryAllCoachInfoByCoachType(parm);
			if (caList.size() != 0) {
				apiResultModel.setMsg("获取所有的教练数据");
				apiResultModel.setData(caList);
				apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
			} else {
				apiResultModel.setMsg("获取所有的教练数据失败！");
				apiResultModel.setData("");
				apiResultModel.setRet(ApiResultModel.RET_ERROR);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	/**
	 * 订单导出
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping("/execl_order_tk")
	public String execlOrderList(HttpServletRequest request , HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("UTF-8");
		String  bool = orderService.execlOrderListByType(request, response, "1");
		return "OK";
	}
	
	/**
	 * 会员购买记录
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/to_order_vip")
	public ModelAndView toOrderVipList(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView model = new ModelAndView();
		Map<String,Object> requestmap = new HashMap<String,Object>();
		Map<String,Object> responsemap = new HashMap<String,Object>();
		String mobile = RequestTools.RequestString(request, "mobile", null);
		int payStatus = RequestTools.RequestInt(request, "payStatus", 0);
		int pageNum = RequestTools.RequestInt(request, "pageNum", 1);
		int vipType = RequestTools.RequestInt(request, "viptype",0);
		String payTime = RequestTools.RequestString(request, "pay_time",""); 
		requestmap.put("mobile", mobile);
		requestmap.put("payStatus", payStatus);
		requestmap.put("pageNum", pageNum);
		requestmap.put("vipType", vipType);
		requestmap.put("payTime",payTime);
		Map<String,Object> responseData = new HashMap<String,Object>();
		try {
			responseData = orderService.getOrderListByHY(requestmap);
			request.getSession().setAttribute("orderListByHY",responseData.get("listAll"));
			model.addAllObjects(responseData);
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*model.addAllObjects(responsemap);*/
		model.setViewName("order/order-vip");
		return model;
	}
	
	
	@RequestMapping("/to_user_account")
	public ModelAndView toUserAccount(HttpServletRequest request, HttpServletResponse response){
		ModelAndView model = new ModelAndView();
		model.setViewName("order/order-account");
		Map<String,Object> requestmap = new HashMap<String,Object>();
		Map<String,Object> responsemap = new HashMap<String,Object>();
		String mobile = RequestTools.RequestString(request, "mobile", null);
		String payStatus = RequestTools.RequestString(request, "payStatus","all");
		int pageNum = RequestTools.RequestInt(request, "pageNo", 1);
		int vipType = RequestTools.RequestInt(request, "viptype",0);
		int userId = RequestTools.RequestInt(request, "userId", 0);
		String payTime = RequestTools.RequestString(request, "pay_time",""); 
		requestmap.put("mobile", mobile);
		requestmap.put("payStatus", payStatus);
		requestmap.put("pageNo", pageNum);
		requestmap.put("vipType", vipType);
		requestmap.put("pay_time",payTime);
		requestmap.put("userId", userId);
		Map<String,Object> responseData = new HashMap<String,Object>();
		model.addAllObjects(userAccountRechargeService.queryUserAccountRechargePageAll(requestmap));
		return model;
	}
	
	/**
	 * 超级管理员退款订单处理
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/admin_createdRefun")
	public String adminCreatedRefundOrder(HttpServletRequest request, HttpServletResponse response) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel();
		String callback = RequestTools.RequestString(request, "callback", "");
		String orderNumber = RequestTools.RequestString(request, "orderNumber", null);
		int biaoshi = RequestTools.RequestInt(request, "biaoshi", 1);
		Map<String, Object> mapWhere = new HashMap<String, Object>();
		
		// 判断该用户是否是管理员
		if(this.getUserInfo().getRoleId() != 2){
			apiResultModel.setData("退款失败");
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setLogined(true);
			apiResultModel.setMsg("退款失败,您不是超级管理员\n没有该权限");
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
		}
		
		OrderInfo orderInfo = orderInfoMapper.queryOrderInfoByNumber(orderNumber.trim());
		// 判断该订单号是否存在
		if(orderInfo == null){
			apiResultModel.setData("退款失败");
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setLogined(true);
			apiResultModel.setMsg("退款失败,找不到该订单,请查证订单号的正确性");
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
		}
		// 全部退款和部分退款提示不能重复提申请退款
		if(OrderContants.PayStatus.REFOUND.equals(orderInfo.getPayStatus()) 
				|| OrderContants.PayStatus.PARTIAL_REFUND.equals(orderInfo.getPayStatus())) {
			apiResultModel.setData("该订单已是退款订单不要重复提交退款申请");
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setLogined(true);
			apiResultModel.setMsg("该订单已是退款订单不要重复提交退款申请");
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
		}
		// 未付款订单不给予退款申请
		if(OrderContants.PayStatus.UNPAID.equals(orderInfo.getPayStatus())) {
			apiResultModel.setData("该订单未付款不能申请退款");
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setLogined(true);
			apiResultModel.setMsg("该订单未付款不能申请退款");
			return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
		}
		mapWhere.put("orderId", orderInfo.getId());
		mapWhere.put("biaoshi", biaoshi);
		int bool = orderService.createdRefundOrder(mapWhere);
		if (bool >= 1) {
			apiResultModel.setData("退款成功");
			apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
			apiResultModel.setLogined(true);
			apiResultModel.setMsg("退款成功");

		} else if(bool == -2){
			apiResultModel.setData("账户余额不足");
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setLogined(true);
			apiResultModel.setMsg("账户余额不足");
			
		}else{
			apiResultModel.setData("退款失败");
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setLogined(true);
			apiResultModel.setMsg("退款失败");
		}

		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
	@ResponseBody
	@RequestMapping("/query_user_order")
	public String queryUserOrderList(HttpServletRequest request,HttpServletResponse response){
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		String callback = RequestTools.RequestString(request, "callback", null);
		Integer userId = RequestTools.RequestInt(request, "userId", 0);
		String mobile = RequestTools.RequestString(request, "mobile", null);
		String nickName = RequestTools.RequestString(request, "nickName", null);
		Integer payStatus = RequestTools.RequestInt(request, "payStatus", 0);
		Integer pageNo = RequestTools.RequestInt(request, "pageNo", 1);
		Integer pageSize = RequestTools.RequestInt(request, "pageSize", 20);
		Map<String,Object> parmaMap = new HashMap<String,Object>();
		parmaMap.put("userId", userId);
		parmaMap.put("mobile", mobile);
		parmaMap.put("nickName", nickName);
		parmaMap.put("payStatus", payStatus);
		parmaMap.put("pageNo", pageNo);
		parmaMap.put("pageSize", pageSize);
		try {
			parmaMap  = orderService.getUserOrderListByUser(parmaMap);
			apiResultModel.setLogined(true);
			apiResultModel.setErrorCode(apiResultModel.RET_SUCCESS);
			apiResultModel.setMsg("查询成功！");
			apiResultModel.setRet(apiResultModel.RET_SUCCESS);
		} catch (Exception e) {
			apiResultModel.setLogined(true);
			apiResultModel.setErrorCode(apiResultModel.RET_ERROR);
			apiResultModel.setMsg("系统运行异常！");
			apiResultModel.setRet(apiResultModel.RET_ERROR);
			
		}
		apiResultModel.setData(parmaMap);
		return ResponseTools.CallBackResultJsonP(JsonTools.jsonSer(apiResultModel), callback);
	}
	
}
